package com.uum.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uum.admin.pojo.entity.SysPermission;
import com.uum.admin.pojo.form.PermissionGenerate;
import com.uum.admin.pojo.query.PermissionPageQuery;
import com.uum.admin.pojo.vo.PermissionVO;
import com.uum.admin.service.SysPermissionService;
import com.uum.common.core.annotation.Operation;
import com.uum.common.core.enums.BusinessType;
import com.uum.web.exception.BizException;
import com.uum.common.core.result.Result;
import com.uum.common.core.result.ResultCode;
import com.uum.web.util.JwtUtils;
import com.uum.common.core.valid.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Api(tags = "权限接口")
@RestController
@RequestMapping("/api/v1/permission")
@RequiredArgsConstructor
public class SysPermissionController {

    private final SysPermissionService permissionService;

    @Operation(description = "查询权限列表",businessType = BusinessType.QUERY,isSaveResponseData = false)
    @ApiOperation("权限列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", defaultValue = "1", value = "页码", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", defaultValue = "10", value = "每页数量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "description", value = "权限名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "menuId", value = "菜单ID", paramType = "query", dataType = "Long")
    })
    @GetMapping("page")
    public Result listPermission(@Validated PermissionPageQuery permissionPageQuery){
        IPage<PermissionVO> result = permissionService.page(permissionPageQuery);
        return Result.success(result.getRecords(), result.getTotal());
    }

    @ApiOperation("根据菜单查询权限列表")
    @GetMapping("list")
    public Result listPermissionByMenuId(@RequestParam(name = "menuId") Long menuId){
        List<SysPermission> permissionList = permissionService.listPermissionByMenuId(menuId);
        return Result.success(permissionList);
    }

    @ApiOperation("权限详情")
    @GetMapping("detail/{id}")
    public Result permissionDetail(@PathVariable Integer id){
        SysPermission permission = permissionService.getById(id);
        return Result.success(permission);
    }

    @Operation(description = "修改权限",businessType = BusinessType.UPDATE)
    @ApiOperation("修改权限")
    @PutMapping("update")
    public Result updatePermission(@RequestBody SysPermission permission) {
//        validPermission();
        boolean result = permissionService.updateById(permission);
        if (result) {
            permissionService.refreshPermRolesRules();
        }
        return Result.judge(result);
    }

    @Operation(description = "授权",businessType = BusinessType.GRANT)
    @ApiOperation("添加权限")
    @PostMapping("add")
    public Result addPermission(@RequestBody SysPermission permission) {
//        validPermission();
        boolean result = permissionService.save(permission);
        if (result) {
            permissionService.refreshPermRolesRules();
        }
        return Result.judge(result);
    }

    @Operation(description = "删除权限",businessType = BusinessType.DELETE)
    @ApiOperation("删除权限")
    @DeleteMapping("delete/{ids}")
    public Result deletePermission(@PathVariable String ids){
//        validPermission();
        boolean result = permissionService.removeByIds(Arrays.asList(ids.split(",")));
        if (result) {
            permissionService.refreshPermRolesRules();
        }
        return Result.judge(result);
    }

    /**
     * 校验权限
     * 非管理员不允许增删改权限
     */
    private void validPermission() {
        if (!JwtUtils.isAdministrator()) {
            throw new BizException(ResultCode.ACCESS_UNAUTHORIZED);
        }
    }

    /**
     * 生成默认的增删改查
     * @return
     */
    @Operation(description = "生成默认权限")
    @ApiOperation("生成默认权限")
    @PutMapping("generatePerm")
    public Result generatePerm(@RequestBody PermissionGenerate generate) {
        permissionService.generatePerm(generate);
        return Result.success();
    }
}

